import { createRouter, createWebHashHistory } from 'vue-router'
import LoginView from '../components/login/LoginView.vue'
import WorkStoreView from '../components/workStore/WorkStoreView.vue'
import { identity } from '../stores/identity';
import HomeView from '../components/workStore/HomeView.vue';
import LeaveMsgView from '../components/workStore/LeaveMsgView.vue';
import InfoView from '../components/workStore/InfoView.vue';
import GoodsView from '../components/workStore/GoodsView.vue';


// import ContactView from '../components/contact/ContactView.vue'

// import Home from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      redirect:"/login"
    },
    {
      path: '/login',
      name: 'login',
      component: LoginView
    },
    {
      path: '/workstore',
      name: 'workstore',
      component: WorkStoreView,
      meta: { requiresAuth: true },
      children: [
        {
          path: 'home',
          name: 'home',
          component: HomeView
        },
        {
          path: 'leaveMsg',
          name: 'leaveMsg',
          component: LeaveMsgView
        },
        {
          path: 'info',
          name: 'info',
          component: InfoView
        },
        {
          path: 'goods',
          name: 'goods',
          component: GoodsView
        },
      ]
    },


  ]
})
router.beforeEach((to, from, next) => {
  console.log(from)
  const requiresAuth = to.matched.some(record => record.meta.requiresAuth);
  const isAuthenticated = identity.isLogin; // 假设你有一个 getter 来检查登录状态
 
  if (requiresAuth && !isAuthenticated) {
    next('/login'); // 如果需要认证但用户未登录，则重定向到登录页面
  } else {
    next(); // 否则继续导航
  }
});
export default router
